package com.zh.chat.gui;

import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import com.zh.chat.controller.ClientContext;

public class TrayManager {
	// 系统脱盘的实现
	private TrayIcon trayIcon = null;
	private SystemTray tray = SystemTray.getSystemTray();
	private ClientContext clientContext = null;

	public void initMainFrameTray(final JFrame frame) {
		URL trayImg = frame.getClass().getClassLoader().getResource("com/zh/chat/image/icon/tray.png");
		Image image = new ImageIcon(trayImg).getImage();
		Image smallImage = new BufferedImage(18, 18, BufferedImage.TYPE_INT_RGB);
		Graphics g = smallImage.getGraphics();
		g.drawImage(image, 0, 0, 18, 18, null);

		PopupMenu pop = new PopupMenu(); // 增加托盘右击菜单
		MenuItem show = new MenuItem("OPEN");
		MenuItem online = new MenuItem("ONLINE");
		MenuItem busy = new MenuItem("BUSY");
		MenuItem away = new MenuItem("AWAY");
		MenuItem hide = new MenuItem("HIDE");
		MenuItem offline = new MenuItem("OFFLINE");
		MenuItem exit = new MenuItem("Exit");
		show.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { // 按下还原键
				tray.remove(trayIcon);
				frame.setVisible(true);
				frame.setExtendedState(JFrame.NORMAL);
				frame.toFront();
			}
		});
		online.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});
		busy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});
		away.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});
		hide.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});
		offline.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

			}
		});
		exit.addActionListener(new ActionListener() { // 按下退出键
			public void actionPerformed(ActionEvent e) {
				clientContext.logout();
				tray.remove(trayIcon);
				System.exit(0);
			}
		});
		pop.add(show);
		pop.add(online);
		pop.add(busy);
		pop.add(away);
		pop.add(hide);
		pop.add(offline);
		pop.add(exit);
		trayIcon = new TrayIcon(smallImage, "SIMCALL", pop);
		trayIcon.setImageAutoSize(true);
		trayIcon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) { // 鼠标器双击事件
				if (e.getClickCount() == 2) {
					tray.remove(trayIcon); // 移去托盘图标
					frame.setVisible(true);
					frame.setExtendedState(JFrame.NORMAL); // 还原窗口
					frame.toFront();
				}
			}
		});

		try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
	
	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}
	
	public void removeTray(){
		tray.remove(trayIcon);
	}

	public void initLoginFrameTray(final JFrame frame) {
		URL trayImg = frame.getClass().getClassLoader().getResource("com/zh/chat/image/icon/tray.png");
		Image image = new ImageIcon(trayImg).getImage();
		Image smallImage = new BufferedImage(18, 18, BufferedImage.TYPE_INT_RGB);
		Graphics g = smallImage.getGraphics();
		g.drawImage(image, 0, 0, 18, 18, null);

		PopupMenu pop = new PopupMenu(); // 增加托盘右击菜单
		MenuItem show = new MenuItem("OPEN");
		MenuItem exit = new MenuItem("Exit");
		show.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { // 按下还原键
				tray.remove(trayIcon);
				frame.setVisible(true);
				frame.setExtendedState(JFrame.NORMAL);
				frame.toFront();
			}
		});
		exit.addActionListener(new ActionListener() { // 按下退出键
			public void actionPerformed(ActionEvent e) {
				tray.remove(trayIcon);
				System.exit(0);
			}
		});
		pop.add(show);
		pop.add(exit);
		trayIcon = new TrayIcon(smallImage, "SIMCALL", pop);
		trayIcon.setImageAutoSize(true);
		trayIcon.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) { // 鼠标器双击事件
				if (e.getClickCount() == 2) {
					tray.remove(trayIcon); // 移去托盘图标
					frame.setVisible(true);
					frame.setExtendedState(JFrame.NORMAL); // 还原窗口
					frame.toFront();
				}
			}
		});

		try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
}
